﻿using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using System;
using System.Runtime.CompilerServices;

namespace IMIForUnity
{
    public class ImiTexture2D
    {

        public enum Type
        {
            COLOR,
            DEPTH,
            USER_DEPTH,
            MAIN_USER_COLOR,
            SUB_USER_COLOR,
            MAIN_USER_DEPTH,
            SUB_USER_DEPTH,
            MAIN_USER_COLOR_CENTERED,
            SUB_USER_COLOR_CENTERED,
        }

        public enum Status
        {
            READY_TO_APPLY,
            READING,
            USED,
        }
        protected Type type;

        internal Status texStatus;
        internal byte[] textureData;
        protected Texture2D texture;
        protected ImiManager imiManager;
        protected ImiResolution resolution;
        public Texture2D Texture { get { return texture; } }

        internal ImiTexture2D(Type type, ImiResolution resolution)
        {
            this.type = type;
            if (type == Type.COLOR)
            {
                texture = new Texture2D(resolution.Width, resolution.Height, TextureFormat.RGB24, false);
                texture.filterMode = FilterMode.Point;
                textureData = new byte[resolution.Width * resolution.Height * 3];
            }
            else
            {
                texture = new Texture2D(resolution.Width, resolution.Height, TextureFormat.RGBA32, false);
                textureData = new byte[resolution.Width * resolution.Height * 4];
            }
            imiManager = ImiManager.GetInstance();
            this.resolution = resolution;
        }

        internal void UpdateTexture()
        {
            if(texStatus == Status.READY_TO_APPLY)
            {
                texture.LoadRawTextureData(textureData);
                texture.Apply();
                texStatus = Status.USED;
            }
        }

        internal void ReleaseTexture()
        {
            texture = null;
            textureData = null;
        }
    }


    internal class ImiTexture2DFactory
    {

        public static ImiTexture2D CreateTexture(ImiTexture2D.Type type)
        {
            ImiPreferences imiPreferences = ImiPreferences.GetInstance();
            ImiTexture2D texture2d = null;
            switch (type)
            {
                case ImiTexture2D.Type.COLOR:
                    texture2d = new ImiTexture2D(type, imiPreferences.colorResolution);
                    break;
                case ImiTexture2D.Type.DEPTH:
                    texture2d = new ImiTexture2D(type, imiPreferences.depthResolution);
                    break;
                case ImiTexture2D.Type.USER_DEPTH:
                    texture2d = new ImiTexture2D(type, imiPreferences.depthResolution);
                    break;
                case ImiTexture2D.Type.MAIN_USER_COLOR:
                    //if (imiPreferences.colorResolution.Size != imiPreferences.depthResolution.Size)
                    //{
                    //    Debug.Log("Getting Main User Color Texture needs the color resolution and depth resolution to be the same!");
                    //}
                    texture2d = new ImiTexture2D(type, imiPreferences.colorResolution);
                    break;
                case ImiTexture2D.Type.SUB_USER_COLOR:
                    //if (imiPreferences.colorResolution.Size != imiPreferences.depthResolution.Size)
                    //{
                    //    Debug.Log("Getting Sub User Color Texture needs the color resolution and depth resolution to be the same!");
                    //}
                    texture2d = new ImiTexture2D(type, imiPreferences.colorResolution);
                    break;
                case ImiTexture2D.Type.MAIN_USER_DEPTH:
                    texture2d = new ImiTexture2D(type, imiPreferences.depthResolution);
                    break;
                case ImiTexture2D.Type.SUB_USER_DEPTH:
                    texture2d = new ImiTexture2D(type, imiPreferences.depthResolution);
                    break;
                case ImiTexture2D.Type.MAIN_USER_COLOR_CENTERED:
                    texture2d = new ImiTexture2D(type, imiPreferences.colorResolution);
                    break;
                case ImiTexture2D.Type.SUB_USER_COLOR_CENTERED:
                    texture2d = new ImiTexture2D(type, imiPreferences.colorResolution);
                    break;
            }
            return texture2d;
        }

    }

}
